#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 10005;
const int maxm = 500005;
int head[maxn],num_edge,n,m,s;
int d[maxn];
bool vis[maxn];
struct node{
	int next;
	int to;
	int dis;
}edge[maxm];
typedef pair<int,int> pii;
priority_queue<pii,vector<pii>,greater<pii> > q;

void add_edge(int from,int to,int dis){
	num_edge++;
	edge[num_edge].next=head[from];
	edge[num_edge].to=to;
	edge[num_edge].dis=dis;
	head[from]=num_edge;
}

void dji(int s){
	d[s]=0;
	q.push(make_pair(d[s],s));
	while(!q.empty()){
		pii tmp=q.top();
		q.pop();
		int x=tmp.second;
		if(vis[x])continue;
		else{
			vis[x]=true;
			for(int i=head[x];i!=0;i=edge[i].next){
				if(d[edge[i].to]>edge[i].dis+d[x]){
					d[edge[i].to]=edge[i].dis+d[x];
					q.push(make_pair(d[edge[i].to],edge[i].to));
				}
			}
		}
	}
}

int main(){
	cin>>n>>m>>s;
	int a,b,c;
	for(int i=1;i<=m;i++){
		cin>>a>>b>>c;
		add_edge(a,b,c);
	}
	for(int i=1;i<=n;i++)d[i]=2147483647;
	dji(s);
	for(int i=1;i<=n;i++){
		if(s==i)cout<<0<<" ";
		else cout<<d[i]<<" ";
	}
	cout<<endl;
	return 0;
}
